package com.fannnnn.system.handler;

import com.alibaba.fastjson.JSON;
import com.fannnnn.system.utils.WebUtils;
import com.fannnnn.system.vo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * \*  Created with IntelliJ IDEA.
 * \*  @author shensifan
 * \*  Date: 2022/1/26
 * \*  Time: 17:55
 * \*  Description:
 * \
 */

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {

        Result result = new Result(HttpStatus.UNAUTHORIZED.value(),"认证失败！",null);

        WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
    }
}
